package myweb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class APPServlet extends HttpServlet {

	/**
	 * 
	 */
	private static JSONArray array=new JSONArray();
	private static JSONObject jsonobject=new JSONObject();
	private static final int BUFFER_SIZE = 1024 * 8;
	private static final long serialVersionUID = 2L;
	private String app_id,device_id,ask;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//根据请求码判断请求类型，做出对应操作
		ask=request.getParameter("ask");
		switch(ask) {
		case "get_devices":
			get_bind_devices(request,response);//
			break;
		case "get_history_byid":
			get_historyby_id(request,response);
			break;
		
		case "bind_device" :
			 bind_device(request,response);
			break;
		case "unbind_device" :
			 unbind_device(request,response);
			break;
		
		}							
	}
	
	private void unbind_device(HttpServletRequest request, HttpServletResponse response) {
		app_id=request.getParameter("app_id");
		device_id=request.getParameter("device_id");
		DBUutil.unbind_id(app_id, device_id);
		try {
			PrintWriter pw=response.getWriter();
			pw.println("success");
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	//绑定设备到user_bind_devices
	private void bind_device(HttpServletRequest request,HttpServletResponse response) {
		// TODO Auto-generated method stub
		app_id=request.getParameter("app_id");
		device_id=request.getParameter("device_id");
		DBUutil.bind_id(app_id, device_id);
		try {
			PrintWriter pw= response.getWriter();
			pw.println("success");
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取指定设备的所有历史消息
	private void get_historyby_id(HttpServletRequest request,HttpServletResponse response) {
	device_id= request.getParameter("device_id");
	JSONArray array= DBUutil.getHistoryInfoByID(device_id);
	response.setContentType("application/json;charset=utf-8");
	try {
		PrintWriter pw=response.getWriter();
		pw.write(array.toString());
		pw.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	//获取绑定的所有设备，以json数组的形式发送出去
	private void get_bind_devices(HttpServletRequest request,HttpServletResponse response) {
		 
		app_id=request.getParameter("app_id");
		Set<String> devices=DBUutil.getBindDevices(app_id);
		//生成迭代器
		Iterator iterator=devices.iterator();
							
		JSONArray array= DBUutil.getCurrentInfoByID(iterator);
		response.setContentType("application/json;charset=utf-8");
		try {
			PrintWriter pw=response.getWriter();
			pw.write(array.toString());
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	public static String read(Reader reader) throws IOException
    {
        StringWriter writer = new StringWriter();
        try
        {
            write(reader, writer);
            return writer.getBuffer().toString();
        }
        finally{ writer.close(); }
    }
	
	public static long write(Reader reader, Writer writer) throws IOException
    {
        return write(reader, writer, BUFFER_SIZE);
    }
	//把缓存器的json数据写入缓存区
	public static long write(Reader reader, Writer writer, int bufferSize) throws IOException
    {
        int read;
        long total = 0;
        char[] buf = new char[BUFFER_SIZE];
        while( ( read = reader.read(buf) ) != -1 )
        {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }
	

}
